<?php

namespace App\Modules\Apicrzs\Http\Controllers;

use App\Events\PayNotifyEvent;
use App\Library\Pay\AliPayService;
use App\Library\Pay\V3WechatPay;
use App\Modules\Apicrzs\Services\Notify\WxNotifyServices;
use App\Modules\Apicrzs\Services\Pay\PayNotifyServices;
use EasyWeChat\Pay\Application as payApp;
use EasyWeChat\Pay\Message;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class PayController extends BaseApicrzsController
{

    public function notifyV3wechat(Request $request)
    {
        V3WechatPay::handleNotify();

    }
    public function notifyAlipay(Request $request)
    {

        AliPayService::handleNotify();
    }
    public function notify(Request $request,$type)
    {
        switch (urldecode($type)) {
            case 'alipay':
                $params_alipay = $request->all();
                xdebug($params_alipay,'支付宝-回调-原始数据','pay/notify/'.$type);
                xdebug($params_alipay,'微信支付-回调-原始数据','pay/notify/v3wechat');
                $out_trade_no = $params_alipay['out_trade_no'];
                $trade_no = $params_alipay['trade_no'];
                $data = [
                    'out_trade_no' => $out_trade_no,
                    'transaction_id' => $trade_no,
                    'desc'=>$params_alipay
                ];

                $payNotifyServices= new PayNotifyServices('water_consume');
                $res =  $payNotifyServices->handleNotify($data,'alipay');
                xdebug($res,'支付宝-回调-处理结果','','pay/notify/'.$type);
                //  if ($res['code']=='SUCCESS'){
                $pay_res = "success";
                // }else{
                //     echo "fail";
                echo "success";
                // }
                exit();
            case 'V3wechat':
                $messagew = json_decode(file_get_contents("php://input"),true);
                xdebug($messagew,'微信-回调-处理结果','','pay/'.$type);

                $wechat = new payApp(getWxConfig('MiniApp'));
                $server = $wechat->getServer();
                // 处理支付结果事件
                $server->handlePaid(function (Message $message,\Closure $next) {
                    $out_trade_no = $message['out_trade_no'];
                    $transaction_id = $message['transaction_id'];
                    $content='39- 微信支付订单号:'.$message['transaction_id'].',商户订单号:'.$message['out_trade_no'].',支付者:'.$message['payer']['openid'].'状态：'.$message['trade_state'];
                    xdebug($content,'微信支付-回调-api','pay/notify/v3wechat');
                    xdebug($message,'微信支付-回调-原始数据','pay/notify/v3wechat');
                    $attach = $message['attach'];
                    $type_attach= $attach['type'];
                    $data = [
                        'out_trade_no' => $out_trade_no,
                        'transaction_id' => $transaction_id,
                        'desc'=>$message
                    ];
                    $payNotifyServices= new PayNotifyServices($type_attach);
                    $res =  $payNotifyServices->handleNotify($data,'weixin');
                });
                // 默认返回 ['code' => 'SUCCESS', 'message' => '成功']
                return $server->serve();
                break;
        }

    }
}
